
/**
 * \file qdx/client/connection.hpp
 *
 * \brief QDX Client Connection Interface
 *
 */

#ifndef __QDXC_CONNECTION_HPP__
#define __QDXC_CONNECTION_HPP__

#include	<string>
#include	<vector>

#include "qdxc/exception.hpp"
#include "qdxc/message.hpp"

namespace QDX
{
    namespace   Client
    {

        /**
         * \class Connection connection.hpp
         *
         * \brief QDX Client Connection Interface
         *
         */
        class Connection {
            public:

                /**
                 * \brief factory method to create (new) a connection instance
                 *
                 * \param flag [in] flags 
                 *
                 * \return connection ptr on success \n
                 * NULL on failure
                 */
                static Connection* create ( int flag = 0 );

                /**
                 * \brief join a public group
                 *
                 * \param group [in] public group name
                 *
                 * \exception on failure
                 */
                virtual void join(const char* group = "QDXC") = 0;

                /**
                 * \brief leave a public group
                 *
                 * \param group [in] public group name
                 *
                 * \exception on failure
                 */
                virtual void leave(const char* group = "QDXC") = 0;

                /**
                 * \brief leave a public group
                 *
                 * \param group [in] public group name
                 * \param joiners [out] list of private-group-names who
                 *                join the public-group-name
                 *
                 * \return >=0 on success (NO. of joiners returned)
                 * \exception on failure
                 */
                virtual int query(const char* group, std::vector<std::string>& joiners) = 0;

                /**
                 * \brief send out a message
                 *
                 * \param message [in] a message to be sent out
                 * \param timeout [in] time out ( in seconds ), (no timeout by default)
                 *
                 * \exception on failure
                 */
                virtual void send(Message& message, int timeout = -1) = 0;

                /**
                 * \brief receive in a message
                 *
                 * \param message [out] a message to received 
                 * \param timeout [in] time out ( in seconds ), (no timeout by default)
                 *
                 * \exception on failure
                 */
                virtual void recv(Message& message, int timeout = -1) = 0;

                /**
                 * \brief connect to QDX NET
                 *
                 * \param hostname [in] hostname ( or IP address ) to be connected to
                 * \param port [in] port to be connected to
                 * \param myname [in] my private name
                 *
                 * \exception on failure
                 */
                virtual void connect(const char* hostname = NULL, const unsigned short port = 0, const char* myname = NULL) = 0;

                /**
                 * \brief disconnect from QDX NET
                 *
                 * \exception on failure
                 */
                virtual void disconnect( void ) = 0;

                virtual ~Connection() {};
        };
    }
}

///////////////////////////////////////////////////////////////////////
//
// ERROR CODE
//
#define	QDXE_BASE	-100

#define	QDXE_CONNECT	(QDXE_BASE + 1)
#define	QDXE_HELLODAEMON	(QDXE_BASE + 2)
#define	QDXE_BYEDAEMON	(QDXE_BASE + 3)
#define	QDXE_HELLO	(QDXE_BASE + 4)
#define	QDXE_BYE	(QDXE_BASE + 5)
#define	QDXE_JOIN	(QDXE_BASE + 6)
#define	QDXE_LEAVE	(QDXE_BASE + 7)
#define	QDXE_QUERY	(QDXE_BASE + 8)


#endif // __QDX_CLIENT_CONNECTION_HPP__

